<?php

class Account_LoginController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
        //$this->_helper->getHelper('Layout')->setLayout('default');
    }
    
    public function indexAction()
    {
         //$this->_forward('someaction', 'somecontroller');
    }
    
    public function showFormAction()
    {
        $show = new Ext_Login_Show();
        $this->view->formLogin = $show->show()->buildLoginForm();
        
        $this->view->referer = Ext_Utility_Misc::referfer();
        
        $config = Ext_Config::getInstance()->getConfig("oauth");
        $adapters = array();
        foreach($config->login as $key=>$val){
            $adapters[] = $val;
        }
        $this->view->adapters = $adapters;
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $account  = $this->filter->filter($this->getRequest()->getPost('account'));
        $password = $this->filter->filter($this->getRequest()->getPost('password'));
        $captcha  = $this->getRequest()->getPost('captcha');
        
        $validater = new Ext_Validate();
        $validater->addValidater(new Add_Account_Login_SavePost_Validate_Account($account));
        $validater->addValidater(new Add_Account_Login_SavePost_Validate_Password($password));
        $validater->addValidater(new Ext_Validate_Adapter_Captcha($captcha));
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $data = new Ext_User_Identify_Data();
        $data->setAccount($account);
        $data->setPassword($password);
        $identify = new Ext_User_Identify($data);
        $identifyResult = $identify->verify();
        if(!$identifyResult->isValid()){
            $viewResult->addMessages($identifyResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $userId  = $identifyResult->getData();

        $oauth = $this->filter->filter($this->getRequest()->getPost('oauth'));
        if($oauth){
            $oauth = new Ext_Oauth_Login();
            $oauth->setBind($userId);
        }
        
        Ext_Auth_Front::getInstance()->auth($userId);

        $viewResult->addMessage($this->translater->_('Login success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
}

